package cn.hengzhu.main_manager.utils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import cn.hengzhu.main_manager.repository.domain.ManageCase;
import cn.hengzhu.main_manager.utils.config.AliMessageConfig;

/**
 * @author liuCheng
 * @since 2018年11月29日 下午6:05:39 阿里短信
 */
public class AliMsmUtils {
	
	private static Logger log = Logger.getLogger(AliMsmUtils.class);
	/**
	 * @author liuCheng
	 * @since 2018年11月30日 上午9:57:44
	 * @return 生成随机四位数
	 */
	public static String getCode() { //
		Integer key = (int) ((Math.random() * 9 + 1) * 1000);
		return key.toString();
	}

	/**
	 * 手机号格式验证
	 * 
	 * @author 周磊
	 * @since 2019年6月28日
	 * @param phone
	 * @return
	 */
	public static boolean isMobile(String phone) {
		if (phone == null || "".equals(phone)) {
			return false;
		}
		Pattern p = null;
		Matcher m = null;
		boolean b = false;
		String s2 = "^[1](([3|5|8][\\d])|([4][4,5,6,7,8,9])|([6][2,5,6,7])|([7][^9])|([9][1,8,9]))[\\d]{8}$";// 验证手机号
		p = Pattern.compile(s2);
		m = p.matcher(phone);
		b = m.matches();
		return b;
	}

	public static boolean sendMessage(String phoneNumber, String code) throws Exception {
		System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
		System.setProperty("sun.net.client.defaultReadTimeout", "10000");

		IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", AliMessageConfig.ACCESSKEY_ID,
				AliMessageConfig.ACCESS_KEY_SECRET);
		DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", AliMessageConfig.PRODUCT, AliMessageConfig.DOMAIN);
		IAcsClient acsClient = new DefaultAcsClient(profile);

		SendSmsRequest request = new SendSmsRequest();
		request.setPhoneNumbers(phoneNumber);
		request.setSignName(AliMessageConfig.SIGN_NAME);
		request.setTemplateCode(AliMessageConfig.TEMPLATE_BIND_CODE);
		Map<String, String> map = new HashMap<>();
		map.put("code", code);
		String jsonString = JSONObject.toJSONString(map);
		request.setTemplateParam(jsonString);
		SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
		String returnCode = sendSmsResponse.getCode();
		if (returnCode.equals("OK")) {
			return true;
		} else {
			return false;
		}
	}
	/**
	 * 储物柜设备状态事件
	 * @param phoneNumber 手机号
	 * @param device 设备
	 * @param time 时间
	 * @param event 事件\状态
	 * @return
	 */
	public static boolean sendMessage(String phoneNumber,String device,Date time,String event) throws Exception {
		System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
		System.setProperty("sun.net.client.defaultReadTimeout", "10000");

		IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", AliMessageConfig.ACCESSKEY_ID,
				AliMessageConfig.ACCESS_KEY_SECRET);
		DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", AliMessageConfig.PRODUCT, AliMessageConfig.DOMAIN);
		IAcsClient acsClient = new DefaultAcsClient(profile);

		SendSmsRequest request = new SendSmsRequest();
		request.setPhoneNumbers(phoneNumber);
		request.setSignName(AliMessageConfig.SIGN_NAME);
		request.setTemplateCode(AliMessageConfig.TEMPLATE_DEVICE_STATE);
		Map<String, String> map = new HashMap<>();
		map.put("device", device);
		map.put("time", DateTimeHelper.formatDateTimetoString(time,DateTimeHelper.FMT_yyyyMMddHHmm));
		map.put("event", event);
		String jsonString = JSONObject.toJSONString(map);
		request.setTemplateParam(jsonString);
		SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
		String returnCode = sendSmsResponse.getCode();
		if (returnCode.equals("OK")) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 储物柜事件短信通知
	 * @param caseNo
	 */
	public static void boxEventNotify(List<ManageCase> mcList,String event) {
		StringBuilder boxName = new StringBuilder();
		if(!mcList.isEmpty() &&  mcList.get(0)!=null){
			ManageCase manageCase = mcList.get(0);
			if(StringUtils.isNotBlank(manageCase.getPlace())){
				boxName.append(manageCase.getPlace());
				if(StringUtils.isNotBlank(manageCase.getCaseNoBuy())){
					boxName.append(manageCase.getCaseNoBuy());
					boxName.append("号柜");
				}
			}else{
				boxName.append(manageCase.getCaseNo());
			}
			log.info(boxName+"储物柜"+event+"!");
			if(Byte.valueOf("1").equals(manageCase.getIsBoxOfflineNotity()) && StringUtils.isNotBlank(manageCase.getBindPhoneNumber())){
				try {
					sendMessage(manageCase.getBindPhoneNumber(),String.valueOf(boxName),new Date(),event);
					log.info("短信通知成功");
				} catch (Exception e) {
					log.info("短信通知失败:");
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
